package com.etrans.web.gps.business.InfoCenterTask.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.InfoCenterTask.domian.EnterpriseTradeVO;
import com.etrans.web.gps.common.MGTemplete;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.AggregateIterable;

@Service
public class InfoCenterService extends BaseService{
	
	/**
	 * 查询信息中心需要发送的手机号码
	 * @return 手机号序列
	 */
	public List<String> queryInfoCenter() {
		return selectList("infoCenterQuery.queryInfoCenter", null);
	}

	/**
	 * 查询业户行业信息
	 * @return 手机号序列
	 */
	public List<EnterpriseTradeVO> queryNowEntTradeInfo() {
		return selectList("infoCenterQuery.queryEntTradeInfo", null);
	}
	
	/**
	 * 查询业户行业信息
	 * @return 手机号序列
	 */
	public List<EnterpriseTradeVO> queryPreEntTrade(Date date) {
		String preTime = new SimpleDateFormat("yyMMdd").format(date);
		//过滤条件
        DBObject match = new BasicDBObject();
        match.put("_id", Pattern.compile("^" + preTime));
        //分组函数
		DBObject group = new BasicDBObject( "_id", "$trade_kind_id");
		group.put("veCount", new BasicDBObject("$sum", 1));
		group.put("acCount", new BasicDBObject("$sum", new BasicDBObject("$toInt", "$has_net")));
		group.put("onCount", new BasicDBObject("$sum", new BasicDBObject("$toInt", "$has_track")));
		//聚合参数
    	List<Bson> pipe = new ArrayList<>();
    	pipe.add(new BasicDBObject( "$match", match));
    	pipe.add(new BasicDBObject( "$group", group));
		AggregateIterable<Document> list = MGTemplete.getCollection("egov", "vehicle_swap").aggregate(pipe);
		List<EnterpriseTradeVO> tradeList = new ArrayList<>();
		for (Document doc : list) {
			EnterpriseTradeVO vo = new EnterpriseTradeVO();
			vo.setTradeKind(doc.getInteger("_id"));
			vo.setVehCount(doc.getInteger("veCount", 0));
			vo.setAcCount(doc.getInteger("acCount", 0));
			vo.setOnCount(doc.getInteger("onCount", 0));
			tradeList.add(vo);
		}
		DBObject distinct = new BasicDBObject("enterprise_id", "$enterprise_id");
		distinct.put("trade_kind_id", "$trade_kind_id");
		DBObject entGroup = new BasicDBObject("_id", "$_id.trade_kind_id");
		entGroup.put("entCount", new BasicDBObject("$sum", 1));
		//聚合参数
    	List<Bson> entPipe = new ArrayList<>();
    	entPipe.add(new BasicDBObject( "$match", match));
    	entPipe.add(new BasicDBObject( "$group", new BasicDBObject("_id", distinct)));
    	entPipe.add(new BasicDBObject( "$group", entGroup));
		AggregateIterable<Document> entList = MGTemplete.getCollection("egov", "vehicle_swap").aggregate(entPipe);
		for (Document doc : entList) {
			for (EnterpriseTradeVO vo : tradeList) {
				if (vo.getTradeKind() == doc.getInteger("_id")) {
					vo.setEntCount(doc.getInteger("entCount", 0));
				}
			}
		}
    	entPipe.clear();
    	entPipe.add(new BasicDBObject( "$match", match));
    	entPipe.add(new BasicDBObject( "$group", new BasicDBObject("_id", "$enterprise_id")));
    	entPipe.add(new BasicDBObject( "$count", "count"));
    	entList = MGTemplete.getCollection("egov", "vehicle_swap").aggregate(entPipe);
		for (Document doc : entList) {
			tradeList.get(0).setCount(doc.getInteger("count", 0));
		}
		return tradeList;
	}
	
	/**
	 * 根据时间发送前一天的
	 * @param date
	 * @return
	 */
	public List<String> getSendFormat(Date date) {
		List<String> params = new ArrayList<String>();
		for (int i = 0; i < 22; i++) {
			params.add("");
		}
		params.set(0, new SimpleDateFormat("MM月dd日").format(date));
		params.set(1, new SimpleDateFormat("yyyy年MM月dd日").format(date));
		//查询业户行业信息
		List<EnterpriseTradeVO> entTrades = this.queryPreEntTrade(date);
		if (entTrades != null && entTrades.size()>0) {
			params.set(2, entTrades.get(0).getCount()+"");
		}
		int totalVe = 0;
		int busCount = 0;
		int totalAc = 0;
		int totalOn = 0;
		int totalEnt = 0;
		int busAcc = 0;
		int busOnline = 0;
		for (EnterpriseTradeVO vo : entTrades) {
			totalVe += vo.getVehCount();
			totalAc += vo.getAcCount();
			totalOn += vo.getOnCount();
			if (vo.getTradeKind() == 1) {
				busCount += vo.getVehCount();
				totalEnt += vo.getEntCount();
				busAcc += vo.getAcCount();
				busOnline += vo.getOnCount();
			}else if(vo.getTradeKind() == 2) {
				busCount += vo.getVehCount();
				totalEnt += vo.getEntCount();
				busAcc += vo.getAcCount();
				busOnline += vo.getOnCount();
			}else if(vo.getTradeKind() == 3) {
				params.set(5, vo.getVehCount() + "");
				params.set(16, vo.getEntCount() + "");
				params.set(17, vo.getVehCount() + "");
				params.set(18, vo.getAcCount() + "");
				params.set(19, vo.getVehCount()==0? 0 + "":Math.ceil(10000.0*vo.getAcCount()/vo.getVehCount())/100 + "%");
				params.set(20, vo.getOnCount() + "");
				params.set(21, vo.getAcCount()==0? 0 + "":Math.ceil(10000.0*vo.getOnCount()/vo.getAcCount())/100 + "%");
			}
		}
		params.set(3, totalVe + "");
		params.set(4, busCount + "");
		params.set(6, totalAc + "");
		params.set(7, totalVe==0? 0 + "":Math.ceil(10000.0*totalAc/totalVe)/100 + "%");
		params.set(8, totalOn + "");
		params.set(9, totalAc==0? 0 + "":Math.ceil(10000.0*totalOn/totalAc)/100 + "%");
		params.set(10, totalEnt + "");
		params.set(11, busCount + "");
		params.set(12, busAcc + "");
		params.set(13, busCount==0? 0 + "":Math.ceil(10000.0*busAcc/busCount)/100 + "%");
		params.set(14, busOnline + "");
		params.set(15, busAcc==0? 0 + "":Math.ceil(10000.0*busOnline/busAcc)/100 + "%");
		return params;
	}
	
}
